﻿/*!
* jQuery DivMenu plugin: 
* 
* version 0.1 2010-03-08
* Requires jQuery v1.3.2 or later
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Authors: Kim Seoung Min
*/
; (function($) {
    if (!$.KDBJQ) {
        $.KDBJQ = new Object();
    };

    $.KDBJQ.kcontextMenu = function(el, liname, event, cssName, callBackFn) {

        var base = this;
        var CssName = cssName;
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("KDBJQ.contextMenu", base);

        base.init = function() {
            base.liname = liname;
            // Put your initialization code here
        };

        // Run initializer
        base.init();

        if (event == undefined) { event = "mousedown" }
        if (CssName == undefined) { CssName = "DivContextMenu" }

        base.$el.bind(event, function(e) {
            $(document.body).find('.kcontextMenu').remove();

            $(document.body).append("<div class='kcontextMenu DivContext "+CssName+"'></div>");
            var menutop = base.$el.offset().top + base.$el.outerHeight();
            var menuleft = base.$el.offset().left;

            $(document.body).find('.kcontextMenu').css("top", menutop);
            $(document.body).find('.kcontextMenu').css("left", menuleft);

            base.$menu = $("#" + base.liname);
            $(document.body).find('.kcontextMenu').append(base.$menu.html());
            
            if(callBackFn != undefined)  {
                $(document.body).find('.kcontextMenu li').bind("click",callBackFn);
            }
            
            if (cssName == 'DivContextInput') {
                $(document.body).find('.kcontextMenu').append("<div class='DivContextClose ui-icon ui-icon-circle-close'></div>");
                $(document.body).find('.DivContextClose').bind("click", function(e) {
                    $(document.body).find('.kcontextMenu').remove();
                }); 
            }
            else {
                $(document.body).find('.kcontextMenu').bind("mouseleave", function(e) {
                    $(document.body).find('.kcontextMenu').remove();
                });              
            }

            

            $(document.body).find('> .kcontextMenu').bind("mouseover", function(e) {
                $(document.body).find('> .kcontextMenu').data("MouseOver", true);
            });
            $(document.body).find('> .kcontextMenu').data("MouseOver", false);

            $(document.body).find('> .kcontextMenu').delay(2000).queue(function() {
                if ($(document.body).find('> .kcontextMenu').data("MouseOver") == false)
                $(document.body).find('> .kcontextMenu').remove();
            });

            return true;
        });

        base.$el.bind("mouseleave", function(e) {
             //$(document.body).find('> .DivContextMenu').animate({"display": "block"},{duration: 3000}).remove();
        });

    };




    $.fn.kcontextMenu = function(liname, event, cssName, callBackFn) {
        return this.each(function() {
            (new $.KDBJQ.kcontextMenu(this, liname, event, cssName,callBackFn));
        });
    };


})(jQuery);